package com.gitee.controller.user;

import com.gitee.context.BaseContext;
import com.gitee.dto.FriendUpdatesDTO;
import com.gitee.dto.FriendUpdatesStatesDTO;
import com.gitee.dto.PageQueryDTO;
import com.gitee.entity.FriendUpdates;
import com.gitee.result.Result;
import com.gitee.service.FriendUpdatesService;
import com.gitee.vo.FriendUpdatesVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "好友动态接口")
@RequestMapping("/user/friendUpdatesController")
@Slf4j
public class FriendUpdatesController {

    @Resource
    private FriendUpdatesService friendUpdatesService;

    @GetMapping("/pageQuery")
    @ApiOperation("好友动态分页查询")
    public Result<List<FriendUpdatesVO>> pageQuery(PageQueryDTO pageQueryDTO) {
        log.info("好友动态分页查询: {}", pageQueryDTO);
        /*
         * 分页查询
         * 每页大小, 第几页
         */
        return Result.success(friendUpdatesService.pageQuery(pageQueryDTO));
    }

    @GetMapping("/getFriendUpdatesVOByTitle/{title}")
    @ApiOperation("模糊查询动态")
    public Result<List<FriendUpdatesVO>> getFriendUpdatesVOByTitle(@PathVariable String title) {
        log.info("模糊查询动态: {}", title);
        return Result.success(friendUpdatesService.getFriendUpdatesVOByTitle(title));
    }

    @PostMapping("/save")
    @ApiOperation("保存动态")
    public Result<?> save(FriendUpdatesDTO friendUpdatesDTO) {
        log.info("保存动态: {}", friendUpdatesDTO);
        friendUpdatesService.save(friendUpdatesDTO);
        return Result.success();
    }

    @GetMapping("/count")
    @ApiOperation("统计当前用户动态数量")
    public Result<Long> count() {
        log.info("统计当前用户动态数量: {}", BaseContext.getCurrentId());
        return Result.success(friendUpdatesService.count(BaseContext.getCurrentId()));
    }

    @PutMapping("/setState")
    @ApiOperation("修改动态点赞等状态")
    public Result<?> setState(@RequestBody FriendUpdatesStatesDTO friendUpdatesStatesDTO) {
        log.info("修改动态点赞等状态: {}", friendUpdatesStatesDTO);
        friendUpdatesService.setState(friendUpdatesStatesDTO);
        return Result.success();
    }

    @GetMapping("/getFriendUpdateById/{friendUpdateId}")
    @ApiOperation("通过id获得动态内容")
    public Result<FriendUpdatesVO> getFriendUpdateById(@PathVariable Integer friendUpdateId) {
        log.info("通过id获得动态内容: {}", friendUpdateId);
        return Result.success(friendUpdatesService.getFriendUpdateById(friendUpdateId));
    }

    @GetMapping("/getFriendUpdateByUserId/{userId}")
    @ApiOperation("通过用户id获取帖子")
    public Result<List<FriendUpdatesVO>> getFriendUpdateByUserId(@PathVariable Integer userId) {
        log.info("通过id获得动态内容: {}", userId);
        return Result.success(friendUpdatesService.getFriendUpdateByUserId(userId));
    }

    @GetMapping("/getCollectionFriendUpdate/{userId}")
    @ApiOperation("获得某用户的收藏")
    public Result<List<FriendUpdatesVO>> getCollectionFriendUpdate(@PathVariable Integer userId) {
        log.info("获得某用户的收藏: {}", userId);
        return Result.success(friendUpdatesService.getCollectionFriendUpdate(userId));
    }

    @GetMapping("/getUserLikeCount/{userId}")
    @ApiOperation("获得用户总获赞数")
    public Result<Long> getUserLikeCount(@PathVariable Integer userId) {
        log.info("获得某用户的收藏: {}", userId);
        return Result.success(friendUpdatesService.getUserLikeCount(userId));
    }

    @DeleteMapping("/deleteFriendUpdate/{friendUpdateId}")
    @ApiOperation("删除动态")
    public Result<?> deleteFriendUpdate(@PathVariable Integer friendUpdateId) {
        log.info("删除动态: {}", friendUpdateId);
        friendUpdatesService.deleteFriendUpdate(friendUpdateId);
        return Result.success();
    }

}
